package com.penghaisoft.microwms.producthand.web.controller.insidewarehouse;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProCountingDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.wms.model.ProCountingTask;
import com.penghaisoft.microwms.product.service.IProCountingTaskService;

/**
 * @ClassName: ProCountingHandController
 * @Description: 盘点手持
 * @author jzh
 * @date 2018年6月13日 上午9:41:25
 */
@RestController
@RequestMapping(value = "/pro/countinghand")
public class ProCountingHandController extends BaseController {

	@Autowired
	private IProCountingTaskService proCountingTaskService;
	
	@Resource
	private StringRedisTemplate stringRedisTemplate;
	
	/**
	 * 查询列表
	 * 
	 * @param pager
	 * @return
	 */
	@PostMapping("list")
	public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows, ProCountingTask proCountingTask) {
		Pager<ProCountingTask> result = proCountingTaskService.findListByCondition(page, rows, proCountingTask);
		return success(result);
	}
	
	
    /** 
    * @Title: countingCodeExist 
    * @Description: 判断盘点单号是否存在
    * @param @param rawBackDto
    * @param @return  参数说明 
    * @return HandResp    返回类型
    * @author jzh 
    * @throws 
    */
    @PostMapping("countingcode")
    public HandResp countingCodeExist(@RequestBody ProCountingDto proCountingDto){
    	HandResp result = new HandResp();
    	Resp resp = proCountingTaskService.loadBill(proCountingDto);
    	result.setCode(resp.getCode());
    	result.setMsg(resp.getMsg());
        return result;
    }

    /** 
    * @Title: barcodeExist 
    * @Description: 扫码盘点
    * @author yanggm 
    * @date 2018年8月21日
    * @throws 
    */
    @PostMapping("barcodeisexist")
    public HandResp barcodeExist(@RequestBody ProCountingDto proCountingDto){
        HandResp result = new HandResp();
//        String LoginName = proCountingDto.getLoginName();
        String key = Constant.PRO_COUNTING_PREFIX + proCountingDto.getBarcode();
        if(stringRedisTemplate.hasKey(key)){
    		result.setCode(Constant.FAIL);
    		result.setMsg("当前条码正在处理中，请不要重复扫描！");
			return result;
    	}
        else{
        	stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
        	try{
        		TransResult transResult = proCountingTaskService.CountingScanBarcode(proCountingDto);
        		result.setCode(transResult.getCode());
				result.setMsg(transResult.getMsg());
				result.setList(transResult.getList());
        	}catch(RuntimeException e){
        		result.setCode(Constant.FAIL);
				result.setMsg(e.getMessage());
        	}finally{
        		stringRedisTemplate.delete(key);
        	}
        }
        return result;
    }
    
    /** 
    * @Title: countingConfig 
    * @Description: 成品盘点确认
    * @author yanggm 
    * @date 2018年8月21日 
    * @throws 
    */
    @PostMapping("countingconfig")
    public HandResp countingConfig(@RequestBody ProCountingDto proCountingDto){
        HandResp result = new HandResp();
        String key = Constant.PRO_COUNTING_PREFIX + proCountingDto.getBarcode();
        if(stringRedisTemplate.hasKey(key)){
    		result.setCode(Constant.FAIL);
    		result.setMsg("请等" + Constant.PRO_HAND_LOST_MINUTES + "分钟再操作！");
			return result;
    	}
        else{
        	stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
        	try{
        		TransResult transResult = proCountingTaskService.complete(proCountingDto);
        		result.setCode(transResult.getCode());
				result.setMsg(transResult.getMsg());
        	}catch(RuntimeException e){
        		result.setCode(Constant.FAIL);
				result.setMsg(e.getMessage());
        	}finally{
        		stringRedisTemplate.delete(key);
        	}
        }
        return result;
    }
}
